package edu.itesm.rm 
{
	import com.greensock.TweenMax;
	import edu.itesm.rm.controls.SquareButton;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.StageDisplayState;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import edu.itesm.rm.face.FacePositionDetector;
	import edu.itesm.rm.scene.ContraScene;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.net.navigateToURL;
	
	/**
	 * ...
	 * @author Aldo Reyes
	 */
	public class NContra extends Sprite
	{
		
		[Embed(source="../../../../assets/medal.png")]		
		private var live:Class;
		
		private var detector:FacePositionDetector;
		private var scene:ContraScene;
		private var stages:XMLList;
		private var currentStage:int = 0;
		private var overlayHolder:Sprite;
		private var lifesHolder:Sprite;
		private var overlayBG:Shape;
		private var _gameEnded:Boolean;
		
		public function NContra() 
		{
			init();
		}
		
		private function init():void {
			_gameEnded = false;
			var loader:URLLoader =  new URLLoader();
			loader.addEventListener(Event.COMPLETE, handleXMLComplete);
			loader.load(new URLRequest("setup.xml"));
		}
		
		private function initScene():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			detector = new FacePositionDetector();
			detector.addEventListener(Event.CHANGE, handleFaceChange);
			detector.init();
			
			
			
			scene = new ContraScene();
			scene.addEventListener(ContraScene.CAMERA_HIT, handleCameraHit, false, 0, true);
			scene.addEventListener(ContraScene.STAGE_COMPLETE, handleStageComplete, false,0,true);
			addChild(scene);
			scene.init();			
			stage.addEventListener(Event.RESIZE, handleResize);
			
			scene.setStage(stages[currentStage]);
			
			
			overlayHolder = new Sprite();
			addChild(overlayHolder);
			
			overlayBG = new Shape();
			overlayBG.graphics.beginFill(0xaa0000);
			overlayBG.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			overlayBG.graphics.endFill();
			overlayHolder.addChild(overlayBG);
			overlayBG.alpha = 0;
			overlayBG.visible = false;
			lifesHolder = new Sprite();
			lifesHolder.x = 50;
			lifesHolder.y = 50;
			overlayHolder.addChild(lifesHolder);
			
			for (var i:int = 0; i < 3; i++) 
			{
				var l:Bitmap = new live();
				l.scaleX = l.scaleY = 3;
				l.x = i * 30;
				lifesHolder.addChild(l);
			}
			
		}
		
		/**********************************
		*Event Handlers
		**********************************/
		private function handleXMLComplete(event:Event):void{
			var loader:URLLoader = URLLoader(event.target);
			stages = new XML(loader.data).stages.stage;
			
			var startBtn:SquareButton = new SquareButton("START GAME");
			startBtn.addEventListener(MouseEvent.CLICK, handleStartGameClick, false, 0, true);
			startBtn.x = (stage.stageWidth-startBtn.width) >> 1;
			startBtn.y = (stage.stageHeight-startBtn.height) >> 1;
			addChild(startBtn);
		}
		
		private function handleStartGameClick(event:MouseEvent):void 
		{
			var btn:SquareButton = SquareButton(event.target);
			removeChild(btn);
			btn.removeEventListener(MouseEvent.CLICK, handleStartGameClick);
			stage.displayState = StageDisplayState.FULL_SCREEN;
			initScene();
		}
		
		private function handleFaceChange(event:Event):void 
		{
			scene.updateCamera(detector.facePercentageX, detector.facePercentageY);
			
		}
		
		private function handleStageComplete(event:Event):void{
			currentStage++;
			currentStage = currentStage%stages.length();
			
			scene.setStage(stages[currentStage]);
		}
		
		
		
		private function handleResize(event:Event):void{
			overlayBG.width = scene.width = stage.stageWidth;
			overlayBG.height = scene.height = stage.stageHeight;			
		}
		
		private function handleRestartClick(event:Event):void 
		{
			
			navigateToURL(new URLRequest("javascript:location.reload(true);"), "_self" );
		}
		
		private function handleCameraHit(event:Event):void 
		{
			if (lifesHolder.numChildren > 0) {
				var l:DisplayObject = lifesHolder.getChildAt(lifesHolder.numChildren - 1);
				TweenMax.to(l, .25, { alpha:0, onComplete:function ():void 
				{
					lifesHolder.removeChild(l);
				} } );
			}else {
				_gameEnded = true;
				var btn:SquareButton = new SquareButton("Restart Game");
				btn.addEventListener(MouseEvent.CLICK, handleRestartClick, false, 0, true);
				btn.x = (stage.stageWidth-btn.width) >> 1;
				btn.y = (stage.stageHeight - btn.height) >> 1;
				addChild(btn);
			}
			scene.enabled = false;
			TweenMax.to(overlayBG, .25, { repeat:2, autoAlpha:1, yoyo:true, onComplete:function() {
					TweenMax.to(overlayBG, .25, { autoAlpha:0, onComplete:function ():void 
					{
						if (!_gameEnded) {
							scene.enabled = true;
						}else {
							TweenMax.to(overlayBG, .25, { autoAlpha:1, tint:0 } );
						}
						
					} } );
				}
			} );
		}
	}

}